﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __357436BD_8380_4b09_B497_D09973F85F65__
#define __357436BD_8380_4b09_B497_D09973F85F65__

#include "../tools/head.h"

/////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos
{
    // 
    // time_impl
    // 
    class time_impl : public xos_stl::mem_item< release_impl< time_impl >, thread_lock >,
        public i_time
    {
    public:
        typedef release_impl< time_impl > T;

    public:
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //

        static int get_addr( POOL *** pppPool, LIST *** pppList );

    public:
        time_impl();
        ~time_impl();

    protected:
        struct timeval m_tmval;
        struct tm m_tm;

    protected:
        int cal_time_zone_second();
        int init_data();

    public:
        int init();
        int term();

    public:
        // 
        // Wed, 28 Aug 2019 03:48:08 GMT
        // 2018-01-30 05:05:30
        // 2018-01-30
        // 
        int get_time_string_ymdhms( char * lpszTimeStr, int nSize, int * lpnRetLen );
        int get_time_string_ymd( char * lpszTimeStr, int nSize, int * lpnRetLen );
        int get_time_string_hms( char * lpszTimeStr, int nSize, int * lpnRetLen );
        int get_time_string_gmt( char * lpszTimeStr, int nSize, int * lpnRetLen );
        int get_time_name_ymdhms( char * lpszTimeStr, int nSize, int * lpnRetLen );
        int get_time_name_ymd( char * lpszTimeStr, int nSize, int * lpnRetLen );
        int get_time_name_hms( char * lpszTimeStr, int nSize, int * lpnRetLen );
        xos_i64 get_wall_time_s();
        int get_milli_second();
        int get_year();
        int get_month();
        int get_week();
        int get_day();
        int get_hour();
        int get_minute();
        int get_second();
        int get_time();

        // 
        // Wed, 28 Aug 2019 03:48:08 GMT
        // 2018-01-30 05:05:30
        // 2018-01-30
        // 
        int set_time_string_ymdhms( const char * lpszTimeStr );
        int set_time_string_ymd( const char * lpszTimeStr );
        int set_time_string_gmt( const char * lpszTimeStr );
        int set_time_name_ymdhms( const char * lpszTimeStr );
        int set_time_name_ymd( const char * lpszTimeStr );
        int set_wall_time_s( xos_i64 tm_s );
        int set_milli_second( int tm_ms );
        int set_year( int tm_y );
        int set_month( int tm_m );
        int set_day( int tm_d );
        int set_hour( int tm_h );
        int set_minute( int tm_m );
        int set_second( int tm_s );

		// 
		// calculate time difference. unit : 1 / 1000 second
		// 
		int diffrent( i_time * pLastTime, xos_i64 * lpulDif );
    };
} // xos

#endif // __357436BD_8380_4b09_B497_D09973F85F65__
